#include <func.h>


int myCd(int netfd, int id, char* args, MYSQL* mysql)
{
    int parentId;
    char tmpPath[256];
    //根据id将当前工作路径取出
    char sql[1024];
    bzero(sql,sizeof(sql));
    bzero(tmpPath,sizeof(tmpPath));
    sprintf(sql,"select * from file_system where id = %d",id);
    int qret = mysql_query(mysql,sql);
    if(qret != 0){
        fprintf(stderr,"SQL statement error: %s\n",mysql_error(mysql));
    }
    MYSQL_RES* res = mysql_store_result(mysql);
    MYSQL_ROW row = mysql_fetch_row(res);
    if(row ==  NULL){
        printf("curWorking-row == 0?\n");
    }
    sprintf(tmpPath,"%s",row[5]);
    printf("tmpPath:%s\n",tmpPath);

    char* substr = strtok(args,"/");
    if(substr == NULL){ //截取子串，cd不给参数i，回到更目录
        //往回走，直到parentId == -1
        parentId = atoi(row[3]);
        while(parentId != -1){
            sprintf(sql,"select * from file_system where id = %d",parentId);
            qret = mysql_query(mysql,sql);
            if(qret != 0){
                fprintf(stderr,"SQL statement error: %s\n",mysql_error(mysql));        
            } 
            res = mysql_store_result(mysql);
            row = mysql_fetch_row(res);
            parentId = atoi(row[3]);
        }
        //此时，已经返回到根目录
        //printf("%s\n",row[5]);
        id = atoi(row[0]);
        return id;
    }
    while(substr != NULL){
        if(strcmp(substr,".") == 0){
            //not toto
        }else if(strcmp(substr,"..") == 0){
            //得到当前行路径的parentID
            sprintf(sql,"select * from file_system where id = %d;",id);
            qret = mysql_query(mysql,sql);
            if(qret != 0){
                fprintf(stderr,"SQL statement error: %s\n",mysql_error(mysql));        
            }
            res = mysql_store_result(mysql);
            row = mysql_fetch_row(res);
            printf("luguo: %s\n",row[5]);
            parentId = atoi(row[3]);
            if(parentId != -1){
                id = parentId;
                //消除末尾的路劲子串
                int len = strlen(tmpPath) - 1;
                while(len >= 0 && tmpPath[len] != '/'){
                  //  printf("%c ",tmpPath[len]);
                    tmpPath[len] = '\0';
                    len--;
                }
                tmpPath[len] = '\0';
                //printf("%c\n",tmpPath[len]);
            }
            //id = parentId;

            printf("..之后的路径 ——> %s\n",tmpPath);
        }else{
            //往下走
            //当前工作目录和子串进行拼接，查文件表，判断是否存在
            sprintf(tmpPath,"%s/%s",tmpPath,substr);
            printf("tmpPath/substr: --> %s\n",tmpPath);
            sprintf(sql,"select * from file_system where path = '%s'",tmpPath);
            qret = mysql_query(mysql,sql);
            if(qret != 0){
                fprintf(stderr,"SQL statement error: %s\n",mysql_error(mysql));        
            }
            res = mysql_store_result(mysql);
            row = mysql_fetch_row(res);
            int rows = mysql_num_rows(res);
            printf("%d\n",rows);
            if(rows == 0){
                printf("不合法的路径:%s\n",tmpPath);
                return 0;   //表示路径不合法
            }
            //路径合法就得改id,
            id = atoi(row[0]);

        }

        substr = strtok(NULL,"/");
    }
    printf("end of id: %d\n",id);
    return id;
}

int main(int argc, char* argv[])
{
    MYSQL* mysql = mysql_init(NULL);
    MYSQL* ret = mysql_real_connect(mysql,"localhost","root","yan888","50test",0,NULL,0);
    if(ret == NULL){
        fprintf(stderr,"mysql connect error:%s\n",mysql_error(mysql));
    }
    char path[128] = "../user2/dir1/../../..";
    myCd(0,8,path,mysql);
    //fprintf(stderr,"SQL statement error: %s\n",mysql_error(mysql));
    return 0;
}

